package com.uenpay.util.app.base;

import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.uenpay.util.R;
import com.uenpay.util.app.ActivityStack;
import com.uenpay.util.common.App;
import com.uenpay.util.rx.RxBus;
import com.uenpay.util.swipeback.SwipeBackLayout;
import com.uenpay.util.swipeback.app.SwipeBackActivity;
import com.uenpay.util.widget.statusbar.StatusBarUtil;

import butterknife.ButterKnife;


/**
 * Created by PS on 2016/11/14.
 */
public abstract class BaseActivity extends SwipeBackActivity {


    /**
     * 日志输出标志
     **/
    protected final String TAG = this.getClass().getSimpleName();

    /**
     * 是否沉浸状态栏
     **/
    protected boolean isSetStatusBar = false;

    /**
     * 是否禁止旋转屏幕
     **/
    protected boolean isAllowScreenRoate = false;

    /**
     * 是否为全屏
     */
    protected boolean isFullScreen = false;

    /**
     * 是否隐藏状态栏
     */
    protected boolean isHideStatuBar = false;

    /**
     * 当前Activity渲染的视图View
     **/
    protected View mContextView = null;

    protected View mTopBarView = null;

    /**
     * 返回键退出
     **/
    protected boolean isBackExit = false;

    /**
     * 返回间隔时间
     */
    private long exitTime;

    private FrameLayout mContentContainView = null;

    private ImageButton btnLeft;
    private Button btnRight;
    private TextView tvTitle;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isHideStatuBar) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    //    requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_base_topbar);
        if (!isFullScreen) initDefaultViews();

        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        StatusBarUtil.setColor(this, getResources().getColor(R.color.statusbar));
        mContentContainView = (FrameLayout) findViewById(R.id.fl_content);
        mTopBarView = findViewById(R.id.rl_top_bar);

        mContextView = getContentView();
        if (isFullScreen) mTopBarView.setVisibility(View.GONE);
        if (mContextView != null)
            mContentContainView.addView(mContextView);
        if (isSetStatusBar) steepStatusBar();
        if (!isAllowScreenRoate) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        ButterKnife.bind(this);
        initView(mContextView);
        ActivityStack.getInstance().push(this);
        getSwipeBackLayout().setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
    }

    /**
     * 绑定视图
     */
    public abstract View bindView();

    /**
     * 绑定布局
     */
    public abstract int bindLayout();

    /**
     * [初始化控件]
     *
     * @param view
     */
    public abstract void initView(final View view);

    /**
     * 沉浸状态栏
     */
    private void steepStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    public void back(View view) {

    }

    public void back() {
        finish();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (isBackExit) {
                if ((System.currentTimeMillis() - exitTime) > 2000) {
                    App.toast("再按一次退出程序");
                    exitTime = System.currentTimeMillis();
                } else {
                    System.exit(0);
                }
            } else {
                back();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.$().unregister(this);
        ActivityStack.getInstance().remove(this);
    }

    /**
     * 获取topbar下面的页面视图
     */
    private View getContentView() {
        return bindView() == null ? LayoutInflater.from(this).inflate(bindLayout(), null) : bindView();
    }

    private void initDefaultViews() {
        btnLeft = (ImageButton) findViewById(R.id.btn_left);
        btnRight = (Button) findViewById(R.id.btn_right);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        btnLeft.setBackgroundResource(R.drawable.back_icon);
        btnRight.setVisibility(View.GONE);

        btnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onLeftBtnInTopBarClick();
            }
        });

        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRightBtnInTopBarClick();
            }
        });

    }

    /**
     * 左边按钮点击监听
     */
    public void onLeftBtnInTopBarClick() {
        back();
    }

    /**
     * 右边按钮点击监听
     */
    public void onRightBtnInTopBarClick() {

    }

    /**
     * 设置左边按钮背景资源
     *
     * @param res
     */
    public void setLeftBtnBackground(int res) {
        if (btnLeft != null) btnLeft.setBackgroundResource(res);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        if (tvTitle != null) tvTitle.setText(title);
    }

    /**
     * 设置标题
     *
     * @param res
     */
    public void setTitle(int res) {
        if (tvTitle != null) tvTitle.setText(res);
    }

    /**
     * 设置右边按钮文字
     *
     * @param text
     */
    public void setRightBtnText(String text) {
        if (btnRight != null) btnRight.setText(text);
        if (btnRight.getVisibility() == View.GONE) btnRight.setVisibility(View.VISIBLE);
    }

    /**
     * 设置右边按钮背景资源
     *
     * @param res
     */
    public void setRightBtnBackGround(int res) {
        if (btnRight != null) btnRight.setBackgroundResource(res);
        if (btnRight.getVisibility() == View.GONE) btnRight.setVisibility(View.VISIBLE);
    }

    public void setBackground(int resId){
        mContentContainView.setBackgroundResource(resId);
    }
}